/*
 * ========THE SOLMIX PROJECT=====================================
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.gnu.org/licenses/ 
 * or see the FSF site: http://www.fsf.org. 
 */

package org.solmix.fmk.base;

import org.solmix.SLXConstants;
import org.solmix.commons.collections.DataTypeMap;
import org.solmix.fmk.config.ConfigRealm;
import org.solmix.fmk.util.OSGIHelper;

/**
 * @author solomon
 * @since 0.0.1
 * @version $ID$ 2011-1-2 solmix-ds
 */
@SuppressWarnings("unchecked")
public abstract class BaseConfig
{

    protected DataTypeMap globalConfig;

    /**
     * Use this method must specify {@link #getConfigRealmName()}.subclass want to reuse this method.must override
     * {@link #getConfigRealmName()}.or not,just return the default realm config.
     * <p>
     * this method used sub class implements {@link #moduleName()} to determine which subtree.sub class use this method should
     * very carefully the return value from {@link #moduleName()}.
     * 
     * @return
     */
    protected DataTypeMap globalConfig() {
        if (globalConfig == null)
            globalConfig = getConfigRealm().getSubtree(moduleName() == null ? "" : moduleName());
        return globalConfig;
    }

    /**
     * 
     * This method used sub class implements {@link #moduleName()} to determine which subtree to get.sub class use this method should
     * very carefully the return value from {@link #moduleName()}.
     * @param realmName configuration realm name.
     * @return
     */
    protected DataTypeMap globalConfig(String realmName) {
        if (globalConfig == null)
            globalConfig = getConfigRealm(realmName).getSubtree(moduleName() == null ? "" : moduleName());
        return globalConfig;
    }

    public static ConfigRealm getConfigRealm(String realmName) {
        return OSGIHelper.getCM(realmName);
    }

    protected ConfigRealm getConfigRealm() {
        return getConfigRealm(getConfigRealmName());
    }

    public String getConfigRealmName() {
        return SLXConstants.MODULE_DS_NAME;
    }

    /**
     * Return module's name that have doze of configuration.
     * 
     * @return
     */
    public abstract String moduleName();

//    /**
//     * convenience sub class to fire Event.
//     * @param event
//     */
//    protected static void fireEvent(Event event) {
//      if(OSGIHelper.getEventAdmin()!=null)
//          OSGIHelper.getEventAdmin().postEvent(event);
//    }
//    /**
//     *  convenience sub class to get  MonitorEventFactory.
//     * @return
//     */
//    protected static MonitorEventFactory getMEFactory(){
//       if(OSGIHelper.getContext()==null)
//           return MonitorEventFactory.getDefault();
//       else
//           return MonitorEventFactory.getInstance(OSGIHelper.getContext());
//    }
}
